package gre.db;

import java.sql.*;
import java.util.*;
/**
 *
 * @author nacuong
 */
public class DBSelector {

  /**
   * Select a wordid using a word
   * @param statement
   * @param word
   * @return wordid that match with string word if found, otherwise return 0
   * @throws SQLException
   */
  public static int selectId(Statement statement, String word) throws SQLException{
    ResultSet results = statement.executeQuery("SELECT * FROM words WHERE word='" + word + "'");
    if (results.next())
      return results.getInt("wid");
    else
      return 0;
  }

  /**
   * Select a word using a wordId
   * @param statement
   * @param wordId 
   * @return
   * @throws SQLException
   */
  public static String selectWord(Statement statement, int wordId) throws SQLException {
    ResultSet results = statement.executeQuery("SELECT * FROM words WHERE wid=" + wordId + "");
    if (results.next()) 
      return results.getString("word");
    else
      return null;
  }

  /**
   * Select a meaning using a wordId
   * @param statement
   * @param wordId
   * @return
   * @throws SQLException
   */
  public static String selectMeaning(Statement statement, int wordId) throws SQLException {
    ResultSet results = statement.executeQuery("SELECT * FROM words WHERE wid=" + wordId + "");
    if (results.next())
      return results.getString("meaning");
    else
      return null;
  }

  /**
   * Select a statement using a wordId
   * @param statement
   * @param wordId
   * @return
   * @throws SQLException
   */
  public static Vector<String> selectStatement(Statement statement, int wordId) throws SQLException {
    Vector<String> sentences = new Vector<String>();
    ResultSet results = statement.executeQuery("SELECT * FROM sentences WHERE wid=" + wordId + "");
    while (results.next())
      sentences.add(results.getString("sentence"));

    return sentences;
  }
}
